package iscript.gameFramework.IsoEngine.display 
{
	import com.greensock.TweenLite;
	import flash.display.Sprite;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.geom.Vector3D;
	import iscript.gameFramework.IsoEngine.data.IsoConfig;
	import iscript.gameFramework.IsoEngine.data.IsoPosition;
	import iscript.gameFramework.IsoEngine.util.IsoUtil;
	
	internal class IsoObject extends Sprite
	{
		
		protected var _position:Vector3D;
		protected var _walk:Boolean;
		protected var _size:Number;
		/**
		 * Y轴
		 */
		protected var _height:Number;		
		/**
		 * Z轴
		 */
		protected var _length:Number;		
		/**
		 * X轴
		 */
		protected var _width:Number;
		protected var _IsoX:Number;
		protected var _IsoY:Number;
		protected var _IsoZ:Number;
		
		protected var _offX:Number;
		protected var _offY:Number;
		
		protected var _innerIsoOffX:Number;
		protected var _innerIsoOffY:Number;
		protected var _innerIsoOffZ:Number;
		
		
		
		public function IsoObject(size:Number=0,width:Number=0,length:Number=0,height:Number=0) 
		{
			_IsoX = 0;
			_IsoY = 0;
			_IsoZ = 0;
			_offX = 0;
			_offY = 0;
			_innerIsoOffX = 0;
			_innerIsoOffY = 0;
			_innerIsoOffZ = 0;
			
			_height = height;			
			_width = width;			
			_length = length;
			if (size >= 2) {
				 _size = size;
				}else {
				 _size = IsoConfig.IsoGridSize;
				}
			_position = new Vector3D();
			updateScreenPosition();
		}
		 
		public function moveTo(xAxis:Number,yAxis:Number,zAxis:Number):void {
			   position = new Vector3D(xAxis * _size, yAxis * _size, zAxis * _size);
		}
		
		public function moveAnimationTo(xAxis:Number, yAxis:Number, zAxis:Number):void {
			   TweenLite.to(position, 3, { x:xAxis * _size, y:yAxis * _size, z:zAxis * _size,onUpdate:moveAnimationToLogic });
			}
			
		private function moveAnimationToLogic():void 
		{
			updateScreenPosition();
		}
		
		/**
		 * 菱形的边的长度(4边相同)
		 */
	/*	public function get edge():int {
			//return _height;
			return _height * IsoUtil.Y_CORRECT;  
			//return Math.round(Point.distance(new Point(0,-_size>>1),new Point(_size,0)));
			}*/
		 
			
		override public function set x(value:Number):void
		{
			_position.x = value;
			updateScreenPosition();
		}
		override public function get x():Number
		{
			return _position.x;
		}
		
		override public function set y(value:Number):void
		{
			_position.y = value;
			updateScreenPosition();
		}
		override public function get y():Number
		{
			return _position.y;
		}
 
		override public function set z(value:Number):void
		{
			_position.z = value;
			updateScreenPosition();
		}
		
		override public function get z():Number
		{
			return _position.z;
		}
		
		/**
		 * 地面面积
		 */
		public function get screenArea():Rectangle {
			  return null;
			}
			
		public function set position(value:Vector3D):void
		{
			_position = value;
			updateScreenPosition();
		}
		
		public function get position():Vector3D
		{
			return _position;
		}
		 
		protected function updateScreenPosition():void
		{
			var position:Vector3D = _position.clone();
				position.x += _IsoX + _innerIsoOffX;
				position.y += _IsoY + _innerIsoOffY;
				position.z += _IsoZ + _innerIsoOffZ;	
			    _position = position;
			var screenPos:Point = IsoUtil.isoToScreen(position);			
			super.x = screenPos.x+_offX;
			super.y = screenPos.y + _offY;
		}
		
 
		/**
		 * 直接定位到空间格子
		 * @return
		 */
		public function setIsoPosition(value:IsoPosition):void{
			    _position.x = value.x * IsoConfig.IsoGridSize;
				_position.y = value.y;
				_position.z = value.z * IsoConfig.IsoGridSize;
				updateScreenPosition();
			}
		
		
		/**
		 * 空间格子定位
		 * @return
		 */
		public function getIsoPosition():IsoPosition {
			   var  pos:IsoPosition =  new IsoPosition()
					pos.x = Math.round(_position.x / IsoConfig.IsoGridSize);
			        pos.y = _height;
			        pos.z = Math.round(_position.z / IsoConfig.IsoGridSize);
			       /* pos.x = Math.round(_position.x / IsoConfig.IsoGridSize) * IsoConfig.IsoGridSize;
			        pos.y = Math.round(_position.y / IsoConfig.IsoGridSize) * IsoConfig.IsoGridSize;
			        pos.z = Math.round(_position.z / IsoConfig.IsoGridSize) * IsoConfig.IsoGridSize;*/
			   return pos;
			}
		
		
		public function get walk():Boolean 
		{
			return _walk;
		}
		
		public function set walk(value:Boolean):void 
		{
			_walk = value;
		}
		
		override public function get height():Number 
		{
			return _height;
		}
		
		override public function set height(value:Number):void 
		{
			_height = value;
			updateScreenPosition();
		}
		
		public function get IsoX():Number 
		{
			return _IsoX;
		}
		
		public function set IsoX(value:Number):void 
		{
			_IsoX = value;
			updateScreenPosition();
		}
		
		public function get IsoZ():Number 
		{
			return _IsoZ;
		}
		
		public function set IsoZ(value:Number):void 
		{
			_IsoZ = value;
			updateScreenPosition();
		}
		
		public function get IsoY():Number 
		{
			return _IsoY;
		}
		
		public function set IsoY(value:Number):void 
		{
			_IsoY = value;
			updateScreenPosition();
		}
		
		public function get offX():Number 
		{
			return _offX;
		}
		
		public function set offX(value:Number):void 
		{
			_offX = value;
			updateScreenPosition();
		}
		
		public function get offY():Number 
		{
			return _offY;
		}
		
		public function set offY(value:Number):void 
		{
			_offY = value;
			updateScreenPosition();
		}
		
		
 
		
		protected function draw():void {}
		
	}

}